<?php
	include('include/header.php');
	include("include/commands.php");
        
        //Check if we are coming to this page from an insert query.
        if ($_POST["fromInsert"] == "true") {
            
            //Instantiate connector.
            $connector = new CloudDBInterface();
           
            //Add the new Schedule now.
            //Get variables.
            $sTime = $_POST["sTime"];
            $eTime = $_POST["eTime"];
            $date = $_POST["date"];
            $tips = $_POST["tips"];
            $employee = $_POST["employee"];

            $employeeId = strtok($employee, ":");

            //Create the query.
            $query =    "INSERT INTO Schedule (startTime, endTime, date, EmployeeId, Tips)
                        VALUES (\"$sTime\", \"$eTime\", \"$date\", $employeeId, $tips)";
            
            //Insert the tuple.
            $connector->insertQuery($query);            
        } 
	  
        if(isset($_GET['sortBy'])) {
		if(!isset($_GET['start']) || !isset($_GET['end'])) {
			$schedule = getScheduleSorted($_GET['sortBy'], $_GET['id']);
		}
		else {
			$schedule = getScheduleSorted($_GET['sortBy'], $_GET['id'], $_GET['start'], $_GET['end']);
		}
		
	}
	else {
		$schedule = getSchedule();
	}
        
?>
        
	<div class="body">
	
                
		<h1> Work Schedule: </h1>
                
                <div class="border">
			<form>
			<p>
			Date From <input type="text" name="start"/>
			Date To <input type="text" name="end"/>(yyyy-mm-dd)
			<br/>
			Sort By:
				<select name="sortBy">
					<option>NONE</option>
					<option>Date</option>
					<option>Employee</option>
				</select>
			<input type="submit" value="Filter"/>
			</p>
			</form>
		</div>
                <br/>
                <div class="border">
                    <form id="newScheduleForm" action="schedule.php" method="post" onsubmit="return validateScheduleInsert();">
                        <h4>Add a New Work Shift</h4>
                        <p>
                            Start Time* <input type="text" name="sTime" id="sTime"/><br/>
                            End Time* <input type="text" name="eTime" id="eTime"/><br/>
                            Date* <input type="text" name="date" id="date"/><br/>
                            Tips* <input type="text" name="tips" id="tips"/><br/>
                            Employee <?php employeeNames(); ?>
                            
                            <input type="hidden" name="fromInsert" id="fromInsert" value="true"/>
                            <input type="submit" value="Submit"/>
                        </p>
                    </form>
                </div>
                <br/>
		<?php 

			if(sizeof($schedule) > 0){
                            
					generateExpensesTable($schedule);
				}
				else{
					echo "<p> No Shift(s) Found. </p>";
				}
				
				function generateExpensesTable($expenses) {
                                    
					echo "<table>";
					echo "<tr>";
					echo "<th>ID</th>";
					echo "<th>Start Time</th>";
					echo "<th>End Time</th>";
                                        echo "<th>Date</th>";
                                        echo "<th>Employee</th>";
                                        echo "<th>Tips</th>";
					echo "</tr>";
                                        generateTableBody($expenses);
					echo "</table>";
				}
		?>
	</div>


<?php 
	include('include/footer.php'); 
?>